const express = require("express")
const router = express.Router()
const time = require("../../utils/time")
const db = require("../../sqlServer/dbConnect")

// 食品原料-- 查询
router.post("/ins/info", (req, res) => {
    const { values,pageConfig,doName } = req.body;
    const {page,pageSize} = pageConfig
    let sql = values === null ? "select * from inspection limit ?,?; select * from inspection" : 
    `select * from inspection where insName=? or insVerify=? or insCreatetime=? limit ?,?;
    select * from inspection where insName=? or insVerify=? or insCreatetime=?`
    const data = values === null ? [(Number(page)-1)*Number(pageSize),Number(pageSize)] : 
    [values.fname,values.fverify,values.insTime,
        (Number(page)-1)*Number(pageSize),Number(pageSize),
        values.fname,values.fverify,values.insTime]
    db.sqlQuery(sql,data,(err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取失败",
                status: 203
            })
        }else {
            res.send({
                data: result[0],
                total:result[1].length,
                msg: "获取成功",
                status: 200
            })
        db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品质量查询",time.getData()])
        }
    })
})

// 食品原料-- 添加
router.post("/ins/add", (req, res) => {
    const { insName, insSmell, insImpurity, insComposition, insHeavyMetal, insBiomarker,insToxicSub,insRemark
        ,insPerson,insCompany,insVerify,insCreatetime,pId,doName } = req.body
    let sql =`insert into inspection(insName, insSmell, insImpurity, insComposition, insHeavyMetal, insBiomarker,insToxicSub,insRemark
        ,insPerson,insCompany,insVerify,insCreatetime,pId) values(?,?,?,?,?,?,?,?,?,?,?,?,?)`
    const data = [insName, insSmell, insImpurity, insComposition, insHeavyMetal, insBiomarker,insToxicSub,insRemark
        ,insPerson,insCompany,insVerify,insCreatetime,pId]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "添加失败！",
                status: 203
            })
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品质量检测信息添加",time.getData()])
        }
    })
})

// 食品原料-- 编辑
router.post("/ins/edit", (req, res) => {
    const { insName, insSmell, insImpurity, insComposition, insHeavyMetal, insBiomarker,insToxicSub,insRemark
        ,insPerson,insCompany,insVerify,insCreatetime,pId, Id,doName} = req.body
    let sql = `update inspection set insName=?, insSmell=?, insImpurity=?, insComposition=?, insHeavyMetal=?, insBiomarker=?,
    insToxicSub=?,insRemark=?,insPerson=?,insCompany=?,insVerify=?,insCreatetime=?,pId=? where Id=?`
    const data = [insName, insSmell, insImpurity, insComposition, insHeavyMetal, insBiomarker,insToxicSub,insRemark
        ,insPerson,insCompany,insVerify,insCreatetime,pId, Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品质量检测信息编辑",time.getData()])
        }
    })
})

// 食品原料-- 删除
router.post("/ins/delete", (req, res) => {
    const { Id,doName } = req.body
    let sql = "delete from inspection where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"食品质量检测信息删除",time.getData()])
        }
    })
})

module.exports = router